package com.megvii.corelib.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.megvii.corelib.components.DateTimePickerView;

/**
 * Created by linyue on 15/12/29.
 */
public class DateTimePickerDialog
{
    private DateTimePickerDialogListener listener;

    public void showDateTimePicker(Context context, long timestamp,
            final DateTimePickerDialogListener listener)
    {
        this.listener = listener;
        final DateTimePickerView picker = new DateTimePickerView(context);
        picker.setDateTime(timestamp);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(picker);
        builder.setTitle("时间选择器");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if (listener != null)
                {
                    listener.onConfirm(picker.getTimetamp());
                }
                dialog.cancel();
            }
        });
        AlertDialog dialog = builder.show();
        dialog.setCanceledOnTouchOutside(true);
    }

    /**
     *
     * @param context
     * @param dateStr 不能包含秒
     * @param listener
     */
    public void showDateTimePicker(Context context, String dateStr,
            final DateTimePickerDialogListener listener)
    {
        long timestamp = DateTimeUtil.getTimestampFromFormattedDateTime(dateStr, false);
        showDateTimePicker(context, timestamp, listener);
    }

    public interface DateTimePickerDialogListener
    {
        public void onConfirm(long timestamp);
    }
}
